package com.hjw.httpclient.demo;

import org.apache.http.HttpEntity;
import org.apache.http.HttpStatus;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import org.junit.Test;

import java.io.*;

/**
 * 爬取网络上的图片保存到本地
 *
 * @author hjwStrart
 * @time 2022-11-10 20:30
 * @version: 1.0
 */
public class Demo4 {

    @Test
    public void method() {
        CloseableHttpClient httpClient = HttpClients.createDefault();
        String url = "https://i0.hdslb.com/bfs/article/de6a79910bfdc9747a618644016581254cefbb9f.png@942w_278h_progressive.webp";
        HttpGet httpGet = new HttpGet(url);
        CloseableHttpResponse httpResponse = null;
        BufferedOutputStream bos = null;
        try {
            httpResponse = httpClient.execute(httpGet);
            int statusCode = httpResponse.getStatusLine().getStatusCode();
            if (HttpStatus.SC_OK == statusCode) {
                System.out.println("请求成功");
                HttpEntity entity = httpResponse.getEntity();
                // 获取 Content-Type
                String contentType = entity.getContentType().getValue();
                System.out.println("Content-Type:" + contentType);
                // 通过 Content-Type 判断相应内容，响应内容如果是图片的话 一定是 image/具体的图片类型
                // image/jpg image/png image/jpeg
                String suffix = ".jpg";
                if (contentType.contains("jpg") || contentType.contains("jpeg")) {
                    suffix = ".jpg";
                } else if (contentType.contains("bmp") || contentType.contains("bitmap")) {
                    suffix = ".bmp";
                } else if (contentType.contains("png")) {
                    suffix = ".png";
                } else if (contentType.contains("gif")) {
                    suffix = ".gif";
                }
                // 图片的 entity 是字节数据
                byte[] bytes = EntityUtils.toByteArray(entity);
                String localPath = "D:" + File.separator + "upload" + File.separator + "img";
                File file = new File(localPath, System.currentTimeMillis() + suffix);
                FileOutputStream fos = new FileOutputStream(file);
                bos = new BufferedOutputStream(fos);
                bos.write(bytes);
                System.out.println(localPath);
            } else {
                System.out.println("请求失败");
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (bos != null) {
                try {
                    bos.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (httpClient != null) {
                try {
                    httpClient.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}
